using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MultiLayer
{
    public class PenTool : Tool
    {
        public PenTool(Control pContainer)
            : base(pContainer)
        {
            this.mCursor = Cursors.Cross;
            this.mPoints = new List<Point>();
            this.mPen = new Pen(Color.Black, 100.0f);
            this.mPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
            this.mPen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round,
                    System.Drawing.Drawing2D.LineCap.Round,
                    System.Drawing.Drawing2D.DashCap.Round);
        }

        public override void StylusDown(Microsoft.StylusInput.RealTimeStylus sender, Microsoft.StylusInput.PluginData.StylusDownData data)
        {
            this.mLastPoint = new Point(data[0], data[1]);
            
            this.mPoints.Clear();
            this.mPoints.Add(this.mLastPoint);

            this.StartDrawing();
        }

        public override void Packets(Microsoft.StylusInput.RealTimeStylus sender, Microsoft.StylusInput.PluginData.PacketsData data)
        {
            for (int i = 0; i < data.Count; i += data.PacketPropertyCount)
            {
                Point curPoint = new Point(data[i], data[i + 1]);
                this.mPoints.Add(curPoint);

                // draw it too
                this.mGraphics.DrawLine(this.mPen, this.mLastPoint, curPoint);
                this.mLastPoint = curPoint;
            }
        }

        public override void InAirPackets(Microsoft.StylusInput.RealTimeStylus sender, Microsoft.StylusInput.PluginData.InAirPacketsData data)
        {
        }

        public override void StylusUp(Microsoft.StylusInput.RealTimeStylus sender, Microsoft.StylusInput.PluginData.StylusUpData data)
        {
            this.mGraphics.DrawLine(this.mPen, this.mLastPoint, new Point(data[0], data[1]));
            this.FinishDrawing();

            // now add it to the screen
            Stroke stroke = new Stroke(this.mPoints.ToArray(), this.mPen);
            Screen.Current.AddEntity(stroke);
        }

        private List<Point> mPoints;
        private Point mLastPoint;
        private Pen mPen;
    }
}
